<?php
require("../global.php");
require("../Phpword/PHPWord.php");
header("Content-Type: text/html; charset=utf-8");

     $surchk = "select * from dordersuremsg where order_id = ".$_REQUEST['order_id']." limit 1";
	 $suchkarr = $db->get_sql_row($surchk,'mysql_fetch_assoc');
    
	if(empty($suchkarr['suredate']))//初次导出确认单的时候就把时间给记录下来。。
	{
		$nowdate = date('Y-m-d');
		$suresql ="update dordersuremsg set suredate ='".$nowdate."' where order_id =".$_REQUEST['order_id']." ";
		
		$db->execute_sql($suresql);
		
		//导出了确认单就更新状态为已经确认
		$sqlorder = "update dordermsg set surests='Y' where order_id =".$_REQUEST['order_id']."  " ;
		$db->execute_sql($sqlorder);
    }
	
     $sures = "select * from dordersuremsg where order_id = ".$_REQUEST['order_id']." limit 1";
	 $suarr = $db->get_sql_row($sures,'mysql_fetch_assoc');
	 
     $query = "select * from dordermsg where order_id=".$_REQUEST['order_id']." limit 1";	
     $orarr = $db->get_sql_row($query,'mysql_fetch_assoc');	 
	 
	 $personalarr = getPerInfo($orarr['login_no'],'tell,fax'); 
	 
	 //行程名称和线路编号
	 $sxlkplan = "SELECT * FROM v1dxlkplanmsg WHERE tuanno = '".$orarr['tuanno']."' limit 1";
	 $xlkarr = $db->get_sql_row($sxlkplan,'mysql_fetch_assoc');
	 
	 //航班信息
	 $sair = "select * from v1dwfairmsg WHERE lc_id = ".$xlkarr['lc_id']." ";
	 $airarr = $db->get_sql_arr($sair,'mysql_fetch_assoc');
	 
	 $sairstr = '';
	 $eairstr = '';
	 
	 if(!empty($airarr))
	 {
	    foreach($airarr as $arow)
		{
		   //航班信息的始发跟线路的出发城市一致就是出发参考航班
		   if($arow['wf_start']== $xlkarr['p_start'])
		   {
		       $sairstr = $arow['wf_hangci'];
		   }else{
		       $eairstr = $arow['wf_hangci'];
		   }
		}
	 }
	 
	 
	 //客人名单
	 $travel = "SELECT t_name FROM v1dtravelmsg WHERE order_id = ".$orarr['order_id']." ";
	 $trarr = $db->get_sql_arr($travel,'mysql_fetch_assoc');
	 
	 if(!empty($trarr))
	 {
	    $travelstr .='';
		
	    foreach($trarr as $trow)
		{
		   $travelstr .= $trow['t_name'].' ';
		}
	 }

        $PHPWord = new PHPWord();
        $section = $PHPWord->createSection(array('marginLeft'=>600, 'marginRight'=>600, 'marginTop'=>600, 'marginBottom'=>600));

        // Add header
        $header = $section->createHeader();
        $table = $header->addTable(array('cellMarginLeft'=>8,'cellMarginRight'=>8));
		
        $table->addRow(600);
        $table->addCell(10000,array('align'=>'center'))->addImage('../images/login/clip_image002.jpg');


        

        // Add footer
        $footer = $section->createFooter();
		$foottext = '上海联系电话：021-51860725 /62458335 /32071217  （一号通永不暂线）         传真：021-32194180';
		$foottext1 = "联系人：刘  慧 QQ：2880136215     陆丽娅QQ：2880136210    徐敏婕QQ：2880136212";
		$foottext2 = "安 莉QQ: 2880136211    张海燕 QQ：2880136213";		
		
        $footer->addPreserveText($foottext, array('align'=>'center'));
        $footer->addPreserveText($foottext1, array('align'=>'center'));
        $footer->addPreserveText($foottext2, array('align'=>'center'));
		
		// Write some text
		$strtext1 = "散客费用确认结算单";
		$PHPWord->addParagraphStyle('pStyle', array('align'=>'center'));
		$PHPWord->addFontStyle('rStyle', array('size'=>'24','bold'=>true));
		
		$section->addText($strtext1,'rStyle', 'pStyle');
        //$section->addTextBreak();
        $fontStyle = array('bold'=>true);
        $fsb = array('bold'=>true,'align'=>'right');
		$fvc = array('valign'=>'center');
		$fac = array('align'=>'center');
		$ccontinue = array('cellMerge' => 'continue');
		$crestart = array('cellMerge' => 'restart','valign'=>'center');
		$crestart1 = array('cellMerge' => 'restart');
		$crestart2 = array('cellMerge' => 'restart','valign'=>'center');
		
		$rcontinue = array('rowMerge' => 'continue');
		$rrestart = array('rowMerge' => 'restart','valign'=>'center');
		$PHPWord->addFontStyle('fStyle', array('bold'=>true));
		
		
        $rowh = '300';
		
		// Define table style arrays
        $styleTable = array('borderSize'=>6, 'borderColor'=>'000000', 'cellMargin'=>80);
        
       // Add table style
        $PHPWord->addTableStyle('myOwnTableStyle', $styleTable);

        $table1 = $section->addTable('myOwnTableStyle');
        $table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('组团社','fStyle','pStyle');
		$customerid = mb_convert_encoding($orarr['customer_id'],'utf-8','GBK');
        $table1->addCell(3000,$fvc)->addText($customerid,$fontStyle);
        $table1->addCell(2000,$fvc)->addText('地 接 社','fStyle','pStyle');
        $table1->addCell(4000,$fvc)->addText('泰华假期',$fontStyle);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('联系人','fStyle','pStyle');
        $table1->addCell(3000,$fvc)->addText(mb_convert_encoding($orarr['cus_name'],'utf-8','GBK'),$fontStyle);
        $table1->addCell(2000,$fvc)->addText('联 系 人','fStyle','pStyle');
        $table1->addCell(4000,$fvc)->addText(mb_convert_encoding($orarr['login_no'],'utf-8','GBK'),$fontStyle);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('电  话','fStyle','pStyle');
        $table1->addCell(3000,$fvc)->addText(mb_convert_encoding($orarr['cus_tell'],'utf-8','GBK'),$fontStyle);
        $table1->addCell(2000,$fvc)->addText('电话/传真','fStyle','pStyle');
		
		$txt1 = "Tel:".$personalarr['tell']." /Fax:".$personalarr['fax'];
		$txt1 = mb_convert_encoding($txt1,'utf-8','GBK');
        $table1->addCell(4000,$fvc)->addText($txt1,$fontStyle);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('传  真','fStyle','pStyle');
        $table1->addCell(3000,$fvc)->addText(mb_convert_encoding($orarr['cus_fax'],'utf-8','GBK'),$fontStyle);
        $table1->addCell(2000,$fvc)->addText('确认日期','fStyle','pStyle');
		$txtdate = $suarr['suredate'];
        $table1->addCell(4000,$fvc)->addText($txtdate,$fontStyle);
		
		
        $section->addTextBreak();
		$strtext1 = "您好";
		$section->addText($strtext1,$fontStyle);
		$strtext1 = "     现将贵社所需定位计划传真与您，请尽快审核确认并预付定金，如有不详敬请来电，谢谢支持！";
		$section->addText($strtext1,$fontStyle);
		
		$table1 = $section->addTable('myOwnTableStyle');
        $table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('出发日期','fStyle','pStyle');
		
		$riqi = date("Y 年 m 月 d 日",strtotime($orarr['begin_date']));    
		
		$hcname = "参考航班:";
		$qchc = (mb_convert_encoding($suarr['sairhc'],'utf-8','GBK')=='') ? mb_convert_encoding($sairstr,'utf-8','GBK') : mb_convert_encoding($suarr['sairhc'],'utf-8','GBK');
		
		$qustr = $riqi .'  '.$hcname . $qchc;
        $table1->addCell(4000,$crestart)->addText($qustr,$fontStyle);
        $table1->addCell(5000,$ccontinue);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('返回日期','fStyle','pStyle');
		
		$riqi = date("Y 年 m 月 d 日",strtotime($orarr['end_date']));    
		
		$hcname = "参考航班:";
		$hchc = (mb_convert_encoding($suarr['eairhc'],'utf-8','GBK')=='') ? mb_convert_encoding($eairstr,'utf-8','GBK') : mb_convert_encoding($suarr['eairhc'],'utf-8','GBK');
		
		$huistr = $riqi .'  '.$hcname . $hchc;
        $table1->addCell(4000,$crestart)->addText($huistr,$fontStyle);
        $table1->addCell(5000,$ccontinue);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('行     程','fStyle','pStyle');

		$xcstr = mb_convert_encoding($xlkarr['line_name'],'utf-8','GBK');
        $table1->addCell(4000,$crestart)->addText($xcstr,$fontStyle);
        $table1->addCell(5000,$ccontinue);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('人     数','fStyle','pStyle');
		
		$renshu = $orarr['adult']+$orarr['child']+$orarr['childno'].'人';    
		
		$renshustr = $renshu .'   '.mb_convert_encoding($travelstr,'utf-8','GBK');
        $table1->addCell(4000,$crestart)->addText($renshustr,$fontStyle);
        $table1->addCell(5000,$ccontinue);
		
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$rrestart)->addText('团队费用','fStyle','pStyle');
		
		$strfy = '';
	   if(!empty($orarr['adult'])) $strfy .= '成人：'.$orarr['adult'].'*'.$orarr['adultp'];
	   if(!empty($orarr['child']))
	   {
	       if($strfy == '')
		   {
		       $strfy .= ' + '.'小孩占床：'.$orarr['child'].'*'.$orarr['childp'];
		   }else{
		       $strfy .=  ' + '.'小孩占床：'.$orarr['child'].'*'.$orarr['childp'];
		   }
	        
	   }	   
	   if(!empty($orarr['childno']))
	   {
	       if($strfy == '')
		   {
		       $strfy .= ' + '.'小孩不占床：'.$orarr['childno'].'*'.$orarr['childnop'];
		   }else{
		       $strfy .= ' + '.'小孩不占床：'.$orarr['childno'].'*'.$orarr['childnop'];
		   }
	   } 
	   
	    $strfy .= ' = '.$orarr['ysmoney'];
	   
	    $feiyong = $strfy.'元';  

        $pri = ($suarr['prempri'] == '') ? 2000: $suarr['prempri'];		
	   
	    $shoufy ="请于". $suarr['premdl']."前付定金".$pri."元/人，出票前付清余款，并请贵社盖公章签字确认！";
	   
	    //$sfy = $feiyong.$section->addTextBreak().$shoufy;
		
        $table1->addCell(4000,$crestart)->addText($feiyong,$fontStyle);
        $table1->addCell(5000,$ccontinue);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$rcontinue);
		
        $table1->addCell(4000,$crestart)->addText($shoufy,$fontStyle);
        $table1->addCell(5000,$ccontinue);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$fvc)->addText('费用不含','fStyle','pStyle');

		$fybh1 = mb_convert_encoding($suarr['fybh1'],'utf-8','GBK');
		$fybh2 = mb_convert_encoding($suarr['fybh2'],'utf-8','GBK');
        $table1->addCell(4000)->addText(trim($fybh1),$fontStyle);
        $table1->addCell(5000)->addText(trim($fybh2),$fontStyle);
		
		$table1->addRow($rowh);
        $table1->addCell(2000,$crestart)->addText('请贵社将团款汇到我社以下账号',$fontStyle);
        $table1->addCell(4000,$ccontinue);
        $table1->addCell(5000,$ccontinue);
		
		$table1->addRow($rowh);
		$gszh = mb_convert_encoding($suarr['gszh'],'utf-8','GBK');
		$srzh = mb_convert_encoding($suarr['srzh'],'utf-8','GBK');
        $table1->addCell(2000,$crestart)->addText(trim($gszh),$fontStyle);
        $table1->addCell(4000,$ccontinue);
        $table1->addCell(5000,$fvc)->addText(trim($srzh),$fontStyle);
		
		$table1->addRow($rowh);
		$tips = mb_convert_encoding($suarr['tips'],'utf-8','GBK');
        $table1->addCell(2000,$crestart)->addText(trim($tips));
        $table1->addCell(4000,$ccontinue);
        $table1->addCell(5000,$ccontinue);
		
		$table1->addRow(600);
        $table1->addCell(2000,$crestart1)->addText('组团社（盖章）：',$fontStyle);
        $table1->addCell(4000,$ccontinue);
        $table1->addCell(5000)->addText('地接社（盖章）：',$fontStyle);

		// Save File
		$filename = $orarr['begin_date'].' '.$orarr['customer_id'].$orarr['cus_name'].'('.$xlkarr['line_name'].')';
		
        $fillexml =$filename.mb_convert_encoding('散客费用确认结算单','GBK','utf-8');
		
		 header("Content-type: application/vnd.ms-word");
         header("Content-Disposition:attachment;filename=".$fillexml.".docx"); 
         header('Cache-Control: max-age=0');

        $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
       // $objWriter->save($filename);
        $objWriter->save('php://output');

?>